/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package bpm.graphic.decorators
{
	import bpm.graphic.DiagramEditor;
	import bpm.graphic.Graph;
	import bpm.graphic.Link;
	import bpm.graphic.LinkAdorner;
	import bpm.graphic.Render;
	import bpm.graphic.ResizeAdorner;
	import bpm.graphic.events.DiagramEditorEvent;
	import bpm.graphic.skins.AdornerRenderSkin;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.IMEConversionMode;
	import flash.ui.Keyboard;
	
	import mx.collections.ArrayCollection;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.FlexMouseEvent;
	import mx.managers.PopUpManager;
	
	import spark.components.Label;
	import spark.components.supportClasses.Skin;
	

	/*******************************************
	 **** huangjixin,2012-10-18,下午5:46:55 **
	 **** 请一句话表述该类主要作用  **
	 *******************************************/
	public class DecoratorPlayer
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		public static var drawLayer:UIComponent;
		
		public static var textEditing:Boolean;
		
		public static var playingAdorner:Boolean;
		
		public static var resizeAdorners:ArrayCollection;
		
		public static var draggingResizeAdorner:Boolean;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private static var _rect:Rectangle;
		
		private static var graph:Graph;
		
		private static var diagram:DiagramEditor;
		
		public function DecoratorPlayer()
		{
		}//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		/**
		 * 绘制矩形。 
		 * @param target
		 * 
		 */
		public static function playRect(target:Rectangle):void{
			drawLayer.graphics.clear();
			drawLayer.graphics.lineStyle(2,0x9A87C7);
			drawLayer.graphics.drawRect(target.x+2,target.y+2,target.width-4,target.height-4);
		}
		
		/**
		 * 清除图层 
		 * 
		 */
		public static function cleanUp():void{
			drawLayer.graphics.clear();
		}
		
		/**
		 * 显示文本。 
		 * @param target
		 * 
		 */
		public static function displayText(target:Render):void{
			textEditing = true;
			var skin:Skin = target.skin as Skin;
			var labelDisplay:Label;
			if(skin.hasOwnProperty("labelDisplay")){
				labelDisplay = skin["labelDisplay"] as Label;
			}
			var displayObject:TextArea;
			displayObject = PopUpManager.createPopUp(labelDisplay, TextArea, false) as TextArea;
			displayObject.width=labelDisplay.width+6;
			
			if(labelDisplay.height > displayObject.height){
				displayObject.height = labelDisplay.height;
			}else{
				displayObject.height=labelDisplay.height+9;
			}
			if(displayObject.width < 36)
				displayObject.width = 36;
			if(displayObject.height < 20)
				displayObject.height = 19;
			displayObject.text=target.label;
			displayObject.setFocus();
//			displayObject.setSelection(0,displayObject.text.length);
			PopUpManager.centerPopUp(displayObject);
			displayObject.x -=2;
			displayObject.y -=4;
			displayObject.imeMode = IMEConversionMode.CHINESE;
			displayObject.addEventListener(MouseEvent.CLICK,function onMouseClick(event:MouseEvent):void{
//				event.stopPropagation();
				displayObject.setFocus();
			});
			displayObject.stage.addEventListener(MouseEvent.MOUSE_DOWN, function onStageMouseDown(event:MouseEvent):void{
				var targ:DisplayObject = event.target as DisplayObject;
				if(event.target["owner"] == displayObject){
					return;
				}
				if(targ && displayObject.contains(targ)){
					return;
				}
				var textInput:TextArea = displayObject;
				if(textInput.text==""){
					textInput.text="请输入文字";
				}
				target.label=textInput.text;
				
				PopUpManager.removePopUp(textInput);
				textEditing = false;
			},true);
			displayObject.addEventListener(KeyboardEvent.KEY_DOWN,function onEnter(event:KeyboardEvent):void{
				if (event.keyCode == Keyboard.ENTER)
				{ 
					var textInput:TextArea = TextArea(event.currentTarget);
					if(textInput.text==""){
						textInput.text="请输入文字";
					}
					target.label=textInput.text;
					
					PopUpManager.removePopUp(textInput);
					textEditing = false;
				}
			});
			displayObject.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, function onMouseDownOutside(event:Event):void
			{
				var textInput:TextArea = TextArea(event.currentTarget);
				if(textInput.text==""){
					textInput.text="请输入文字";
				}
				target.label=textInput.text;
				PopUpManager.removePopUp(textInput);
				textEditing = false;
			});
			displayObject.addEventListener(FlexEvent.ENTER,function onMouseDownOutside(event:Event):void
			{
				var textInput:TextArea = TextArea(event.currentTarget);
				if(textInput.text==""){
					textInput.text="请输入文字";
				}
				target.label=textInput.text;
				PopUpManager.removePopUp(textInput);
				textEditing = false;
			});
		}
		
		public static function displayAdorner(target:Render):ResizeAdorner{
			if(!resizeAdorners){
				resizeAdorners = new ArrayCollection();
			}
			
			var adorner:ResizeAdorner;
			if(target is Link){
				adorner = new LinkAdorner(target);
			}else if(target is Render){
				adorner = new ResizeAdorner(target);
			}
			
			resizeAdorners.addItem(adorner);
			return adorner;
		}
		
		public static function getAdornerByRender(target:Render):ResizeAdorner{
			for each (var adorner:ResizeAdorner in resizeAdorners) 
			{
				if(adorner){
					if(adorner.adornedObject == target){
						break;
					}
				}
			}
			
			return adorner;
		}
		
		/**
		 * 移除边饰。 
		 * 
		 */
		public static function clearAdorners():void{
			for each (var adorner:ResizeAdorner in resizeAdorners) 
			{
				if(adorner){
					adorner.cleanUp(adorner);
				}
			}
		}
		
		/**
		 * 移除边饰。 
		 * 
		 */
		public static function clearAdorner(adorner:ResizeAdorner):void{
			adorner.cleanUp(adorner);
		}
		
		/**
		 *  
		 * @param adorner
		 * 
		 */
		public static function deleteAdorner(adorner:ResizeAdorner):void{
			if(resizeAdorners.contains(adorner)){
				resizeAdorners.removeItemAt(resizeAdorners.getItemIndex(adorner));
			}
		}
		
		public static function drawRect(diagramEditor:DiagramEditor):void{
			if(!drawLayer){
				return;
			}
			
			if(!_rect){
			 _rect = new Rectangle(0,0,0,0);
			}
			
			if(!diagram){
				diagram = diagramEditor;
			}
			
			diagram.addEventListener(MouseEvent.MOUSE_DOWN, function onDrawLayerMouseDown(event:MouseEvent):void{
				_rect.x = drawLayer.mouseX;
				_rect.y = drawLayer.mouseY;
				
				diagram.stage.addEventListener(MouseEvent.MOUSE_UP,onDrawLayerMouseUp);
				diagram.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			});
		}
		
		
		protected static function onDrawLayerMouseUp(event:MouseEvent):void
		{
			diagram.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			diagram.stage.removeEventListener(MouseEvent.MOUSE_UP,onDrawLayerMouseUp);
			
			cleanUp();
			
			/*if (_rect.width > 0 && _rect.height > 0){
				
			}else if (_rect.width < 0 && _rect.height < 0){
				_rect.x = drawLayer.mouseX;
				_rect.y = drawLayer.mouseY;
				_rect.height *= -1;
				_rect.width *= -1;
			}else if (_rect.width > 0 && _rect.height < 0){
				_rect.y -= _rect.height * (-1);
				_rect.height *= -1; 
			}else{
				_rect.x -= _rect.width * (-1);
				_rect.width *= -1;
			}
			
			diagram.setNodesSelected(_rect);*/
		}
		
		protected static function enterFrameHandler(event:Event):void
		{
			/*_rect.width = drawLayer.mouseX - _rect.x;
			_rect.height = drawLayer.mouseY - _rect.y;
			playRect(_rect);*/
			
			var xPlus:Number = drawLayer.mouseX - _rect.x;            
			var yPlus:Number = drawLayer.mouseY - _rect.y;
			if (xPlus > 0 && yPlus > 0){
				_rect.width = xPlus;
				_rect.height = yPlus;
				playRect(_rect);
				diagram.setNodesSelected(_rect);	
			}else{
				cleanUp();
			}
		}
		/*public static function drawRect(graph:Graph):void{
			graph.addEventListener(MouseEvent.MOUSE_DOWN,onGraphMouseDown);
			
		}
		
		protected static function onGraphMouseDown(event:MouseEvent):void
		{
			var graph:Graph = event.currentTarget as Graph;
			
			graph.stage.addEventListener(MouseEvent.MOUSE_MOVE,onGraphMouseMove);
			graph.stage.addEventListener(MouseEvent.MOUSE_UP,onGraphMouseUp);
		}
		
		protected static function onGraphMouseMove(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
		}
		
		protected static function onGraphMouseUp(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
		}*/
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
	}//类结束
}//包结束